//
//  WTPictureDetailViewController.m
//  OneDay
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTPictureDetailViewController.h"
#import "WTRequestManager.h"
#import <UIImageView+WebCache.h>

@interface WTPictureDetailViewController ()

// 图片的数组
@property (nonatomic, strong) NSMutableArray * imgArray;

// 描述内容的数组
@property (nonatomic, strong) NSMutableArray * descArray;
// 图片,文字的scrollView
@property (nonatomic, strong) UIScrollView * scrollView;

@end

@implementation WTPictureDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
    self.tabBarController.tabBar.hidden = YES;

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self requestData];
    
    
    _imgArray = [NSMutableArray array];

    // 创建导航栏左边按钮
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"m-fanhui"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButtonClick:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    // Do any additional setup after loading the view.
}
// 返回方法
- (void)leftButtonClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark -- 网络请求
- (void)requestData
{
    
    NSString * url = [NSString stringWithFormat:@"http://api.wap.miercn.com/api/2.0.3/pic_arc.php?plat=android&proct=mierapp&versioncode=20150807&apiCode=4&id=%@",_ID];
    [WTRequestManager requestWithUrlString:url parDic:nil method:GET success:^(NSData *data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       
        
        _imgArray = dic[@"imgurls"];
     
        _descArray = dic[@"desc"];
    
        [self showPicture];
    } error:^(NSError *error) {
        
    }];
}


// 图片展示,文字的展示
- (void)showPicture
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 210 * kRelativelength_Y, kScreenWidth, kScreenHeight - 210 * kRelativelength_Y - 49)];
    _scrollView.contentSize = CGSizeMake(_imgArray.count * kScreenWidth, _scrollView.height);
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.alwaysBounceHorizontal = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:_scrollView];
    
    for (int i = 0; i < _imgArray.count; i++) {
        
        UIImageView * imageV = [[UIImageView alloc]initWithFrame:CGRectMake(i * kScreenWidth, 0, _scrollView.width, 300 * kRelativelength_Y)];
        
        [imageV sd_setImageWithURL:[NSURL URLWithString:_imgArray[i]]];
        [_scrollView addSubview:imageV];
        
        // 题目
        UILabel  *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(i * kScreenWidth, imageV.bottom + 10 * kRelativelength_Y, kScreenWidth, 30 * kRelativelength_Y)];
        titleLabel.textColor = [UIColor whiteColor];
        NSString * title = [NSString stringWithFormat:@"%d/%ld %@", i + 1, (unsigned long)_imgArray.count, _detailTitle];
        titleLabel.text = title;
        [_scrollView addSubview:titleLabel];
        
        
        // 自适应高度
        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
        CGRect fram = [_descArray[i] boundingRectWithSize:CGSizeMake(kScreenWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(i * kScreenWidth, titleLabel.bottom + 10 * kRelativelength_Y,kScreenWidth, fram.size.height)];
        
        contentLabel.numberOfLines = 0;
        contentLabel.font = [UIFont systemFontOfSize:14];
        contentLabel.textColor = [UIColor whiteColor];
        contentLabel.text = _descArray[i];
      
        
        [_scrollView addSubview:contentLabel];
        
    }
    

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
